package com.kuizii.demo.core.config;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;


public class IdModShardingAlgorithm implements StandardShardingAlgorithm<String> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        for (String s : collection) {
            String lastNum = preciseShardingValue.getValue().substring(preciseShardingValue.getValue().length() - 1);
            Integer mod= Integer.parseInt( lastNum)%2;
            if (s.endsWith( mod.toString())) {
                return s;
            }
            continue;
        }
        return "sys_user";
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<String> rangeShardingValue) {
        return null;
    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return "MOD1";
    }
}
